// ErrorList.cpp : implementation file
//

#include "stdafx.h"
#include "DSTapeMount.h"
#include "ErrorList.h"

#include "Util\MinUtilExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CErrorList dialog


CErrorList::CErrorList(CWnd* pParent /*=NULL*/)
	: CDialog(CErrorList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CErrorList)
	//}}AFX_DATA_INIT
	m_nTotalItemCount = 0;
}


void CErrorList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CErrorList)
	DDX_Control(pDX, IDB_LISTPGUP, m_bListPGUP);
	DDX_Control(pDX, IDB_LISTPGDN, m_bListPGDN);
	DDX_Control(pDX, IDB_EXIT, m_bExit);
	DDX_Control(pDX, IDB_SEARCH, m_bSearch);
	DDX_Control(pDX, IDC_DATETIME_SEARCH, m_ctrlSearch);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_ERRORCODE, m_ErrorListCtrl);
}

BEGIN_MESSAGE_MAP(CErrorList, CDialog)
	//{{AFX_MSG_MAP(CErrorList)
	ON_BN_CLICKED(IDB_LISTPGUP, OnListpgup)
	ON_BN_CLICKED(IDB_LISTPGDN, OnListpgdn)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	ON_BN_CLICKED(IDB_SEARCH, OnSearch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErrorList message handlers

void CErrorList::OnListpgup() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30) * -1;

	m_ErrorListCtrl.Scroll(m_ScrollSize);
}
  
void CErrorList::OnListpgdn() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30);

	m_ErrorListCtrl.Scroll(m_ScrollSize);
}

void CErrorList::OnExit() 
{
	// TODO: Add your control notification handler code here
	m_ErrorListCtrl.DeleteAllItems();

	CDialog::OnCancel();
}

BOOL CErrorList::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if( pMsg->hwnd == GetDlgItem(IDC_ERRORCODE)->m_hWnd ) 
	{
		if( pMsg->message == WM_LBUTTONDOWN ) 
			 pMsg->message = 0;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CErrorList::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetWindowText("Error History");
	ButtonInitial();
	ListCtrlInit();

	m_ErrorListCtrl.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
	m_strDirectoryName = _T(".\\data\\ErrorData\\");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CErrorList::ListCtrlInit()
{
	LV_COLUMN lvcolumn;
	//LV_ITEM lvitem;
 
	char* Title[3] = {"No", "Error List", "Error Time"};
	int width[3] = {30,550,115};

	m_ErrorListCtrl.DeleteAllItems();

	for(int i=0;i<3;i++)
	{
		lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
		lvcolumn.fmt = LVCFMT_LEFT;
		lvcolumn.pszText = Title[i];
		lvcolumn.iSubItem = i;
		lvcolumn.cx = width[i];
		m_ErrorListCtrl.InsertColumn(i,&lvcolumn);
	}
	return TRUE;
}

void CErrorList::ButtonInitial()
{
	// Function Button
	m_bExit.SetIcon(IDI_EXIT, 32, 32);
	m_bExit.SetImagePos(CPoint(MINBUTTON_CENTER,5));
	m_bExit.SetTextPos(CPoint(MINBUTTON_CENTER,40));
	m_bExit.SetTextSize(10,TRUE);

	// Up/Down Button
	m_bListPGUP.SetIcon(IDI_PAGE_UP, 32, 32);
	m_bListPGUP.SetImagePos(CPoint(MINBUTTON_CENTER, 8));
	m_bListPGUP.SetTextPos(CPoint(MINBUTTON_CENTER, 40));

	m_bListPGDN.SetIcon(IDI_PAGE_DOWN, 32, 32);
	m_bListPGDN.SetImagePos(CPoint(MINBUTTON_CENTER, 8));
	m_bListPGDN.SetTextPos(CPoint(MINBUTTON_CENTER, 40));

	m_bSearch.SetIcon(IDI_SEARCH, 32, 32);
	m_bSearch.SetImagePos(CPoint(MINBUTTON_CENTER,5));
	m_bSearch.SetTextPos(CPoint(MINBUTTON_CENTER,40));
	m_bSearch.SetTextSize(10,TRUE);
}


void CErrorList::OnSearch() 
{
	// TODO: Add your control notification handler code here
 	m_ErrorListCtrl.DeleteAllItems();
	
	//	LV_COLUMN lvcolumn;
	LV_ITEM lvitem;
	CTime		m_pTime;
	int	nYear, nMonth, nDay;
	CString strFileName = _T("");
	
	m_ctrlSearch.GetTime(m_pTime);
	nYear = m_pTime.GetYear();
	nMonth = m_pTime.GetMonth();
	nDay = m_pTime.GetDay();
	
	strFileName.Format(_T("%s%04d%02d%02d.dat"),m_strDirectoryName,nYear,nMonth,nDay);
	
	FILE* fp;

	if((fp = fopen(strFileName, "r")) == NULL)
	{
		MyMessageBox("Error List File that does not read.","Read Error!", M_ICONERROR);
		return;
	}
	
	lvitem.mask = LVIF_TEXT;
	
	char buf[1000];
	char buf_1[1000]; 
	char buf_2[1000];
	char tmp[1000];
	char* p;
	
	for(int i=0; i<1000 ;i++)
	{
		fgets( buf,1000,fp );
		if( feof(fp) ) break;
		
		lvitem.iItem = i;
		lvitem.iSubItem = 0;
		sprintf(tmp,"%d",i+1);
		lvitem.pszText = tmp;
		m_ErrorListCtrl.InsertItem(&lvitem);
		
		p = strtok( buf,"," );
		if(p != NULL) strcpy(buf_1, p);
		else strcpy(buf_1, "");
		
		p = strtok(NULL,"\n");
		if(p != NULL) strcpy(buf_2,p);
		else strcpy(buf_2, "");
		
		lvitem.iSubItem = 1;
		sprintf(tmp,"%s",buf_1);
		lvitem.pszText = tmp;
		m_ErrorListCtrl.SetItem(&lvitem);
		
		lvitem.iSubItem = 2;
		sprintf(tmp,"%s",buf_2);
		lvitem.pszText = tmp;
		m_ErrorListCtrl.SetItem(&lvitem);
	}
	
	fclose(fp);
	m_nTotalItemCount = m_ErrorListCtrl.GetItemCount();
	
	CRect tmpRect;
	m_ErrorListCtrl.GetItemRect(0, &tmpRect, LVIR_LABEL);
	m_nColumHeight = tmpRect.bottom - tmpRect.top;
}
